define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'coupon/index' + location.search,
                    add_url: 'coupon/add',
                    edit_url: 'coupon/edit',
                    del_url: 'coupon/del',
                    multi_url: 'coupon/multi',
                    import_url: 'coupon/import',
                    table: 'coupon',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('name'), operate: 'LIKE'},
                        {field: 'type', title: __('type'), formatter: Table.api.formatter.label, searchList: {0: __('通用券'), 1: __('注册赠券'), 2: __('会员月卡充值赠劵'), 3: __('会员季卡充值赠劵'), 4: __('会员年卡充值赠劵')}},
                        {field: 'goods_type', title: __('Goods_type'), formatter: Table.api.formatter.label, searchList: {0: __('全商品'), 1: __('品类'), 2: __('商品')}},
                        // {field: 'desc', title: __('desc'), operate: 'LIKE'},
                        // {field: 'tag', title: __('Tag'), operate: 'LIKE'},
                        // {field: 'total', title: __('Total')},
                        {field: 'discount', title: __('Discount'), operate:'BETWEEN'},
                        {field: 'min', title: __('Min'), operate:'BETWEEN'},
                        // {field: 'status', title: __('status')},
                        // {field: 'goods_value', title: __('Goods_value'), operate: 'LIKE'},
                        // {field: 'code', title: __('code'), operate: 'LIKE'},
                        // {field: 'time_type', title: __('Time_type')},
                        // {field: 'days', title: __('Days')},
                        // {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'deleted', title: __('Deleted')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });


            

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(document).on("change", 'select[name="row[goods_type]"]', function(){
                type = $(this).val()
                // 0=全商品，1=分类，2=商品 
                if(type == 0){
                    $("#goods").hide();
                    $("#category").hide();
                }else if(type == 1){
                    $("#goods").hide();
                    $("#category").show();
                }else if(type == 2){
                    $("#goods").show();
                    $("#category").hide();
                }
            });
            Controller.api.bindevent();
        },
        edit: function () {

            var type = $('select[name="row[goods_type]"]').val();
             // 0=全商品，1=分类，2=商品 
             if(type == 0){
                $("#goods").hide();
                $("#category").hide();
            }else if(type == 1){
                $("#goods").hide();
                $("#category").show();
            }else if(type == 2){
                $("#goods").show();
                $("#category").hide();
            }

            $(document).on("change", 'select[name="row[goods_type]"]', function(){
                type = $(this).val()
                // 0=全商品，1=分类，2=商品 
                if(type == 0){
                    $("#goods").hide();
                    $("#category").hide();
                }else if(type == 1){
                    $("#goods").hide();
                    $("#category").show();
                }else if(type == 2){
                    $("#goods").show();
                    $("#category").hide();
                }
                
                console.log($(this).val())
                //后续操作
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});